/*
 * $Id: mod_s2h.cpp 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <stdio.h>
#include <string.h>

#include <tcl.h>
#include <srchilite/sourcehighlight.h>

#include <iostream>
#include <sstream>

using namespace std;
using namespace srchilite;

int
init_s2h(ClientData cdata, Tcl_Interp *interp, int argc, Tcl_Obj *const *argv)
{
   int retval = TCL_ERROR;
   int length = 0;
   std::string outlang = "xhtml";

   std::string program = Tcl_GetStringFromObj(argv[0], &length);
   std::string infile = Tcl_GetStringFromObj(argv[1], &length);
   std::string inlang = Tcl_GetStringFromObj(argv[2], &length);
   cdata = cdata;
   argc = argc;

   const std::string outputLang = outlang + ".outlang";
   const std::string inputLang = inlang + ".lang";
   srchilite::SourceHighlight s2h(outputLang);
   std::istringstream in(infile.c_str(), std::istringstream::in);
   std::ostringstream out("");

   s2h.highlight(in, out, inputLang);
   const std::string result = out.str();

   if (result.length()) {
      Tcl_SetObjResult(interp, Tcl_NewStringObj(
               result.c_str(), result.length()));
      retval = TCL_OK;
   }

   return retval;
}

